using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Text;
using System.Text.RegularExpressions;

namespace Pscx.Commands.TabExpansion.Handlers
{
    class ParenthesesExpressionHandler : ExpressionHandlerBase
    {
        public ParenthesesExpressionHandler(GetTabExpansionCommand command)
            : base(command)
        {

        }

        public override void TryExpand(string line, string word)
        {
            Match match = _pattern.Match(line);

            if (match.Success)
            {
                string parens = match.Groups["Parentheses"].Value;
                string expr = match.Groups["Expression"].Value;

                if (ConfirmInvocation(parens))
                {
                    int lastSpace = line.LastIndexOf(' ');
                    
                    string resultPrefix = line.Substring(lastSpace + 1);
                    
                    int lastDot = resultPrefix.LastIndexOf('.');
                    resultPrefix = resultPrefix.Substring(0, lastDot + 1);

                    ExpandExpression(parens, expr, resultPrefix);
                }
            }
        }

        private static readonly Regex _pattern = new Regex(@"(?x)
            ^
            (?<Parentheses> \( (.+) \) )
            \.
            (?<Expression> .*)");
    }
}
